# 2024/4/18 下午6:54 HFL
import os
import sys
import time
import threading
from loguru import logger


# 日志工具类
class LoggerUtils(object):
    def __init__(self):
        # 上锁，防止重复写入日志
        self.lock = threading.Lock()
    # 获取当前文件的根路径路径
    LOGGER_PATH = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'log/')
    LOGGER_FORMAT_Console = ("<green>{time:YYYY-MM-DD HH:mm:ss,SSS}</green> <red>[{thread}]</red> <blue>{level}</blue> "
                             " <green>方法:{function}</green> <yellow>{message}</yellow>")
    LOGGER_FORMAT = "{time:YYYY-MM-DD HH:mm:ss} [{thread}]{line} {message}"
    IS_FILE_LOGGER = True

    def logger(self, is_file_logger=IS_FILE_LOGGER):
        with self.lock:
            logger.remove()

            if is_file_logger:
                # 文件输出
                logger.add(self.LOGGER_PATH + '{time:YYYY-MM-DD}.log',
                           format=self.LOGGER_FORMAT, rotation="00:00",
                           retention='10 days',
                           colorize=True,
                           enqueue=True,
                           encoding='utf-8'
                           )

            # 控制台输出
            logger.add(sys.stdout, colorize=True, level='DEBUG', format=self.LOGGER_FORMAT_Console)
            return logger

#loggerlog = LoggerUtils()
#.logger().info('你好')